package com.neomanager.admin.web;

import java.util.ArrayList;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.neomanager.admin.dao.MenuDao;
import com.neomanager.login.model.AdminInfo;
import com.util.UtilCommon;

@Controller
@SessionAttributes("adminInfo")
public class MenuController
{
	private final Logger logger = Logger.getLogger(this.getClass());
	@Resource(name="MenuDao")
	MenuDao menu;
	
	/**
	 * 메뉴관리 화면
	 */
	@RequestMapping(value = "/admin/menumng_list.do", method = RequestMethod.GET)
	public String listMenumng(Model model, 
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.info("메뉴관리 화면");
	    HttpSession session = request.getSession();
	    AdminInfo aInfo = (AdminInfo) session.getAttribute( "adminInfo" );
		String id = aInfo.getAdmin_id();
		String menuFile = "../menu/" + id + "_menu.jsp";

		ArrayList<Map<String, Object>> listMap = menu.selectMenuInfo();	
		
		model.addAttribute("menuinclude", menuFile);
		model.addAttribute("adminInfo", aInfo);
		model.addAttribute("listMap", listMap);

		return "admin/menumng_list";
	}

	/**
	 * 메뉴관리 등록/수정/삭제 처리
	 */	
	@RequestMapping(value = "/admin/menumng_proc.do", method = RequestMethod.POST)
	public String processMenumng(@RequestParam Map<String, Object> commandMap, Model model, 
			HttpServletRequest request, HttpServletResponse response)
	{
		logger.debug("메뉴관리 등록/수정/삭제 처리");
		if(commandMap.get("mode").toString().equals("write"))
		{
			String sys_id = UtilCommon.idMake("");
			String menupath = "";
			String menupathname = "";

			if(commandMap.get("menu_path").toString().equals(""))
			{
				menupath = sys_id;
				menupathname = commandMap.get("menu_name").toString();
			}
			else
			{
				menupath = commandMap.get("menu_path").toString() + "_" + sys_id;
				menupathname = commandMap.get("menu_pathname").toString() + "_" + commandMap.get("menu_name").toString();
			}

			commandMap.put("menu_sid", sys_id);
			commandMap.put("menu_path", menupath);
			commandMap.put("menu_pathname", menupathname);
			
			menu.insertMenuInfo(commandMap);
		}
		else if(commandMap.get("mode").toString().equals("edit"))
		{
			menu.updateMenuInfo(commandMap);
		}
		else if(commandMap.get("mode").toString().equals("delete"))
		{
			menu.deleteMenuInfo(commandMap);
		}
		
		return "redirect:../admin/menumng_list.do";
	}
	
}
